## Check that we are able to dump MIPS PLT GOT entries using -A properly.

# RUN: yaml2obj --docnum=1 %s -o %t.plt.o
# RUN: llvm-readobj -A %t.plt.o | FileCheck %s --check-prefix=LLVM
# RUN: llvm-readelf -A %t.plt.o | \
# RUN:   FileCheck %s --strict-whitespace --match-full-lines --check-prefix=GNU

# LLVM:      PLT GOT {
# LLVM-NEXT:   Reserved entries [
# LLVM-NEXT:     Entry {
# LLVM-NEXT:       Address: 0x2000
# LLVM-NEXT:       Initial: 0x0
# LLVM-NEXT:       Purpose: PLT lazy resolver
# LLVM-NEXT:     }
# LLVM-NEXT:     Entry {
# LLVM-NEXT:       Address: 0x2008
# LLVM-NEXT:       Initial: 0x0
# LLVM-NEXT:       Purpose: Module pointer
# LLVM-NEXT:     }
# LLVM-NEXT:   ]
# LLVM-NEXT:   Entries [
# LLVM-NEXT:     Entry {
# LLVM-NEXT:       Address: 0x2010
# LLVM-NEXT:       Initial: 0x0
# LLVM-NEXT:       Value: 0x0
# LLVM-NEXT:       Type: None (0x0)
# LLVM-NEXT:       Section: Undefined (0x0)
# LLVM-NEXT:       Name: foo (5)
# LLVM-NEXT:     }
# LLVM-NEXT:     Entry {
# LLVM-NEXT:       Address: 0x2018
# LLVM-NEXT:       Initial: 0x0
# LLVM-NEXT:       Value: 0x0
# LLVM-NEXT:       Type: None (0x0)
# LLVM-NEXT:       Section: Undefined (0x0)
# LLVM-NEXT:       Name: bar (1)
# LLVM-NEXT:     }
# LLVM-NEXT:   ]
# LLVM-NEXT: }

#       GNU:PLT GOT:
# GNU-EMPTY:
#  GNU-NEXT: Reserved entries:
#  GNU-NEXT:   Address  Initial Purpose
#  GNU-NEXT:  0000000000002000 0000000000000000 PLT lazy resolver
#  GNU-NEXT:  0000000000002008 0000000000000000 Module pointer
# GNU-EMPTY:
#  GNU-NEXT: Entries:
#  GNU-NEXT:   Address  Initial Sym.Val. Type    Ndx Name
#  GNU-NEXT:  0000000000002010 0000000000000000 0000000000000000 NOTYPE  UND foo
#  GNU-NEXT:  0000000000002018 0000000000000000 0000000000000000 NOTYPE  UND bar
#   GNU-NOT:{{.}}

--- !ELF
FileHeader:
  Class:   ELFCLASS64
  Data:    ELFDATA2LSB
  Type:    ET_REL
  Machine: EM_MIPS
Sections:
  - Name:    .rel.plt
    Type:    SHT_REL
    Flags:   [ SHF_ALLOC ]
    Address: 0x1000
    Link:    .dynsym
    Relocations:
      - Offset: 0x1
        Symbol: 1
        Type:   R_MIPS_JUMP_SLOT
      - Offset: 0x2
        Symbol: 2
        Type:   R_MIPS_JUMP_SLOT
  - Name:    .got.plt
    Type:    SHT_PROGBITS
    Flags:   [ SHF_WRITE, SHF_ALLOC ]
    Address: 0x2000
    Size:    32 ## (dynamic symbols number + 2) * 8
  - Name: .dynamic
    Type: SHT_DYNAMIC
    Entries:
      - Tag:   DT_JMPREL
        Value: 0x1000
      - Tag:   DT_MIPS_PLTGOT
        Value: 0x2000
DynamicSymbols:
  - Name:   "foo"
  - Name:   "bar"

## Check we report errors when dynamic tags, needed for dumping PLT, are missing.

# RUN: yaml2obj --docnum=2 -DTAG=DT_MIPS_PLTGOT %s -o %t.err1.o
# RUN: llvm-readobj -A %t.err1.o 2>&1 | FileCheck %s -DFILE=%t.err1.o --check-prefixes=NO-OUTPUT,ERR1

# RUN: yaml2obj --docnum=2 -DTAG=DT_JMPREL %s -o %t.err2.o
# RUN: llvm-readobj -A %t.err2.o 2>&1 | FileCheck %s -DFILE=%t.err2.o --check-prefixes=NO-OUTPUT,ERR2

--- !ELF
FileHeader:
  Class:   ELFCLASS64
  Data:    ELFDATA2LSB
  Type:    ET_EXEC
  Machine: EM_MIPS
Sections:
  - Name: .dynamic
    Type: SHT_DYNAMIC
    Entries:
      - Tag:   [[TAG]]
        Value: 0
      - Tag:   DT_NULL
        Value: 0

## Check we report errors when we are unable to find PLTGOT/JMPREL sections.
# RUN: yaml2obj --docnum=3 %s -DVAL1=0xffff -o %t.err3.o
# RUN: llvm-readobj -A %t.err3.o 2>&1 | FileCheck %s -DFILE=%t.err3.o -check-prefixes=NO-OUTPUT,ERR3

# RUN: yaml2obj --docnum=3 %s -DVAL2=0xffff -o %t.err4.o
# RUN: llvm-readobj -A %t.err4.o 2>&1 | FileCheck %s -DFILE=%t.err4.o -check-prefixes=NO-OUTPUT,ERR4

--- !ELF
FileHeader:
  Class:   ELFCLASS64
  Data:    ELFDATA2LSB
  Type:    ET_EXEC
  Machine: EM_MIPS
Sections:
  - Name: .dynamic
    Type: SHT_DYNAMIC
    Entries:
      - Tag:   DT_MIPS_PLTGOT
        Value: [[VAL1=0]]
      - Tag:   DT_JMPREL
        Value: [[VAL2=0]]
      - Tag:   DT_NULL
        Value: 0
  - Name:    .foo
    Type:    SHT_PROGBITS
    Address: 0x100
    ShSize:  0xffffffff
    Link:    [[LINK=0x1]]
DynamicSymbols: []

## Check we report errors when we are unable to dump PLTGOT properly.

# RUN: yaml2obj --docnum=3 -DVAL1=0x100 %s -o %t.err5.o
# RUN: llvm-readobj -A %t.err5.o 2>&1 | FileCheck %s -DFILE=%t.err5.o -check-prefixes=NO-OUTPUT,ERR5

# RUN: yaml2obj --docnum=3 -DVAL2=0x100 -DLINK=0xaaaaaaaa %s -o %t.err6.o
# RUN: llvm-readobj -A %t.err6.o 2>&1 | FileCheck %s -DFILE=%t.err6.o -check-prefixes=NO-OUTPUT,ERR6

# RUN: yaml2obj --docnum=3 -DVAL2=0x100 %s -o %t.err7.o
# RUN: llvm-readobj -A %t.err7.o 2>&1 | FileCheck %s -DFILE=%t.err7.o -check-prefixes=NO-OUTPUT,ERR7

# NO-OUTPUT:      LoadName: <Not found>
# NO-OUTPUT-NEXT: There is no .MIPS.abiflags section in the file.
# NO-OUTPUT-NEXT: There is no .MIPS.options section in the file.
# NO-OUTPUT-NEXT: There is no .reginfo section in the file.

# ERR1-NEXT: warning: '[[FILE]]': cannot find JMPREL dynamic tag
# ERR2-NEXT: warning: '[[FILE]]': cannot find MIPS_PLTGOT dynamic tag
# ERR3-NEXT: warning: '[[FILE]]': there is no non-empty PLTGOT section at 0xffff
# ERR4-NEXT: warning: '[[FILE]]': there is no non-empty RELPLT section at 0xffff
# ERR5-NEXT: warning: '[[FILE]]': unable to read PLTGOT section content: section [index 2] has a sh_offset (0x70) + sh_size (0xffffffff) that is greater than the file size (0x280)
# ERR6-NEXT: warning: '[[FILE]]': unable to get a symbol table linked to the SHT_PROGBITS section with index 2: invalid section index: 2863311530
# ERR7-NEXT: warning: '[[FILE]]': unable to get a string table for the SHT_DYNAMIC section with index 1: invalid sh_type for symbol table, expected SHT_SYMTAB or SHT_DYNSYM

# NO-OUTPUT-EMPTY:
# NO-OUTPUT-NOT: {{.}}

## Check how we print PLT entries when they are unnamed section symbols.
# RUN: yaml2obj --docnum=4 %s -o %t3
# RUN: llvm-readobj -A %t3 2>&1 | FileCheck %s -DFILE=%t3 --check-prefix=SEC-SYMS-LLVM
# RUN: llvm-readelf -A %t3 2>&1 | FileCheck %s -DFILE=%t3 --check-prefix=SEC-SYMS-GNU

# SEC-SYMS-LLVM:      PLT GOT {
# SEC-SYMS-LLVM:        Entries [
# SEC-SYMS-LLVM:          Entry {
# SEC-SYMS-LLVM:            Section: Absolute (0xFFF1)
# SEC-SYMS-LLVM-NEXT: warning: '[[FILE]]': unable to get section index for symbol with st_shndx = 0xfff1 (SHN_ABS)
# SEC-SYMS-LLVM-NEXT:       Name: <?> (0)
# SEC-SYMS-LLVM-NEXT:     }
# SEC-SYMS-LLVM-NEXT:     Entry {
# SEC-SYMS-LLVM:            Section: .got.plt (0x2)
# SEC-SYMS-LLVM-NEXT:       Name: .got.plt (0)
# SEC-SYMS-LLVM-NEXT:     }
# SEC-SYMS-LLVM-NEXT:     Entry {
# SEC-SYMS-LLVM:            Section: Common (0xFFF2)
# SEC-SYMS-LLVM-NEXT: warning: '[[FILE]]': unable to get section index for symbol with st_shndx = 0xfff2 (SHN_COMMON)
# SEC-SYMS-LLVM-NEXT:       Name: <?> (0)
# SEC-SYMS-LLVM-NEXT:     }
# SEC-SYMS-LLVM-NEXT:     Entry {
# SEC-SYMS-LLVM:            Type: Section (0x3)
# SEC-SYMS-LLVM-NEXT: warning: '[[FILE]]': found an extended symbol index (4), but unable to locate the extended symbol index table
# SEC-SYMS-LLVM-NEXT:       Section: Reserved (0xFFFF)
# SEC-SYMS-LLVM-NEXT:       Name: <?> (0)
# SEC-SYMS-LLVM-NEXT:     }
# SEC-SYMS-LLVM-NEXT:   ]
# SEC-SYMS-LLVM-NEXT: }

# SEC-SYMS-GNU:      PLT GOT:
# SEC-SYMS-GNU:       Entries:
# SEC-SYMS-GNU-NEXT:   Address          {{.*}} Ndx Name
# SEC-SYMS-GNU-NEXT: warning: '[[FILE]]': unable to get section index for symbol with st_shndx = 0xfff1 (SHN_ABS)
# SEC-SYMS-GNU-NEXT:   0000000000002010 {{.*}} ABS <?>
# SEC-SYMS-GNU-NEXT:   0000000000002018 {{.*}}   2 .got.plt
# SEC-SYMS-GNU-NEXT: warning: '[[FILE]]': unable to get section index for symbol with st_shndx = 0xfff2 (SHN_COMMON)
# SEC-SYMS-GNU-NEXT:   0000000000002020 {{.*}} COM <?>
# SEC-SYMS-GNU-NEXT: warning: '[[FILE]]': found an extended symbol index (4), but unable to locate the extended symbol index table
# SEC-SYMS-GNU-NEXT:   0000000000002028 {{.*}} RSV[0xffff] <?>

--- !ELF
FileHeader:
  Class:   ELFCLASS64
  Data:    ELFDATA2LSB
  Type:    ET_REL
  Machine: EM_MIPS
Sections:
  - Name:    .rel.plt
    Type:    SHT_REL
    Flags:   [ SHF_ALLOC ]
    Address: 0x1000
    Link:    .dynsym
    Relocations:
      - Offset: 0x1
        Symbol: 1
        Type:   R_MIPS_JUMP_SLOT
      - Offset: 0x2
        Symbol: 2
        Type:   R_MIPS_JUMP_SLOT
      - Offset: 0x2
        Symbol: 3
        Type:   R_MIPS_JUMP_SLOT
      - Offset: 0x3
        Symbol: 4
        Type:   R_MIPS_JUMP_SLOT
  - Name:    .got.plt
    Type:    SHT_PROGBITS
    Flags:   [ SHF_WRITE, SHF_ALLOC ]
    Address: 0x2000
    Size:    48 ## (dynamic symbols number + 2) * 8
  - Name: .dynamic
    Type: SHT_DYNAMIC
    Entries:
      - Tag:   DT_JMPREL
        Value: 0x1000
      - Tag:   DT_MIPS_PLTGOT
        Value: 0x2000
DynamicSymbols:
  - Type:    STT_SECTION
    Index:   SHN_ABS
  - Type:    STT_SECTION
    Section: .got.plt
  - Type:    STT_SECTION
    Index:   SHN_COMMON
  - Type:    STT_SECTION
    Index:   SHN_XINDEX
